package com.zian.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.zian.Interceptor.AdminAuthInterceptor;
import com.zian.Interceptor.UserAuthInterceptor;
import com.zian.common.JsonObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    private final AdminAuthInterceptor adminAuthInterceptor;

    private final UserAuthInterceptor userAuthInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminAuthInterceptor)
                .excludePathPatterns("/admin/admins/login", "/common/**", "/user/admins/logout" ,"/payCallBack/notify/alipay")
                .addPathPatterns("/admin/**");

        registry.addInterceptor(userAuthInterceptor)
                .excludePathPatterns(
                        "/user/users/login",
                        "/user/users/register",
                        "/user/users/logout",
                        "/common/**",
                        "/user/carousels/list",
                        "/user/index-configs/list",
                        "/payCallBack/notify/alipay",
                        "/user/pay/notify"
                )
                .addPathPatterns("/user/**");
    }

    // 将我们自定的转换器添加进行消息转换列表中 ， 拓展自己消息转换器
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter mc = new MappingJackson2HttpMessageConverter();
        mc.setObjectMapper(new JsonObjectMapper());
        //   如果将自定义的消息转换放在 1 第一， 可能到 swagger 生成的文档无法访问
        converters.add(1, mc);
    }

    /**
     * 设置静态资源映射
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将 upload 文件夹的资源映射到本地的文件下 , 否则无法访问
        // file:///E:/MyClass/newbee-mall/upload/0b1e57bf-b4fd-40df-9832-4749d7d69db9.jpg
        // 文件本地存储
        // registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uploadPath);

        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}
